<template>
  <div>
    <div class="handle-box">
      <!-- <el-button type="primary" class="handle-del mr10" @click="addTable">新增</el-button> -->
      <el-date-picker class="mr10" v-model="query.time" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                      value-format="yyyy-MM-dd">
      </el-date-picker>
      <el-select class="mr10" :style="{'width':'200px'}" v-model="query.status" placeholder="状态">
        <el-option label="全部状态" value=""></el-option>
        <el-option label="审核通过" value="2"></el-option>
        <el-option label="审核不通过" value="3"></el-option>
      </el-select>
      <el-input v-model="query.kw" placeholder="请输入姓名/电话查询" class="handle-input mr10"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="getData">搜索</el-button>
    </div>
    <el-table v-loading="loading" :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column type="index" :index="tableIndex" width="55" label="序号" align="center"></el-table-column>
      <el-table-column prop="chName" label="姓名" width="80"></el-table-column>
      <el-table-column prop="chPhone" label="电话" width="130"></el-table-column>
      <el-table-column label="擅长产品类别" width="155">
        <template slot-scope="scope">
          <el-tag class="tagChan" v-for="item in scope.row.typeNames">{{item}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="chCreateTime" label="创建时间" width="180"></el-table-column>
      <el-table-column prop="chCompanyName" label="公司名称" width="200"></el-table-column>
      <el-table-column prop="chCompanyAddress" label="公司地址" width="200"></el-table-column>
      <el-table-column prop="chIntroduce" label="自我介绍" width="200"></el-table-column>
      <el-table-column label="账号状态" align="center" fixed="right" width="150">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.chIsOpen" active-text="正常" active-value="1" inactive-text="停用" inactive-value="0" active-color="#13ce66"
                     inactive-color="#ff4949" @change="dtChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" fixed="right" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.chStatus == 0 || scope.row.chStatus == 1">待审核</el-tag>
          <el-tag v-if="scope.row.chStatus == 2" type="success">审核通过</el-tag>
          <el-tag v-if="scope.row.chStatus == 3" type="danger">审核不通过</el-tag>
          <el-tooltip v-if="scope.row.chStatus == 3" :content="scope.row.chFailReason" placement="bottom" effect="light">
            <i class="el-icon-warning-outline" :style="{color:'red',marginLeft:'5px'}"></i>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="180" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.chStatus == 0 || scope.row.chStatus == 1">
            <el-button type="text" icon="el-icon-view" :loading="scope.row.loadOk" @click="reviewOk(scope.row)">审核通过</el-button>
            <el-button type="text" icon="el-icon-view" @click="reviewNo(scope.row)" :loading="scope.row.loadNo">审核不通过</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination background layout="total, prev, pager, next" :current-page="query.page" :page-size="query.limit" :total="query.totalCount"
                     :pager-count="5" @current-change="handlePageChange" @prev-click="handlePageChange" @next-click="handlePageChange">
      </el-pagination>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="审核不通过" :visible.sync="refuseFlg" width="30%" :close-on-click-modal="false">
      <el-form ref="form" label-width="120px">
        <el-form-item label="审核不通过原因">
          <el-input type="textarea" v-model="refuseText"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="refuseFlg = false">取 消</el-button>
        <el-button type="primary" @click="refuseClick" :loading="refuseId.loadNo">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 停用账号原因 -->
    <el-dialog title="停用原因" :visible.sync="dtFlg" width="30%" :close-on-click-modal="false">
      <el-form ref="form" label-width="120px">
        <el-form-item label="停用原因">
          <el-input type="textarea" v-model="dtText"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dtFlg = false">取 消</el-button>
        <el-button type="primary" @click="dtChangeClick" :loading="dtLoad">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { channelInit, channelStatus, channeluserStatus } from '../../../../api/index';
export default {
  data() {
    return {
      loading: false,
      activeName: '0',
      tableData: [],
      query: {
        page: 1,
        limit: 10,
        time: [],
        status: '',
        kw: '',
        totalCount: 0,
      },

      refuseId: '',
      refuseFlg: false,
      refuseText: '',

      dtid: '',
      dtFlg: false,
      dtLoad: false,
      dtText: '',

    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取列表
    getData() {
      this.loading = true;
      let query = {
        page: this.query.page,
        limit: this.query.limit,
        kw: this.query.kw,
        status: this.query.status,
        startTime: this.query.time ? this.query.time[0] : '',
        endTime: this.query.time ? this.query.time[1] : '',
      }
      channelInit(query).then((res) => {
        this.loading = false;
        if (res.code === 0) {
          res.recordList = res.recordList.map(item => {
            item.chIsOpen = String(item.chIsOpen)
            item.loadOk = false;
            item.loadNo = false;
            return item;
          })
          this.tableData = res.recordList;
          this.query.totalCount = res.totalCount;
        } else {
          this.$message(res.msg)
        }
      });
    },
    //点击审核通过
    reviewOk(item) {
      this.$confirm('确认审核通过吗？', '审核操作', {
        type: 'warning'
      }).then(() => {
        item.loadOk = true;
        this.reviewAjax(item, '2');
      })
    },
    //点击审核不通过
    reviewNo(item) {
      this.refuseId = item;
      this.refuseFlg = true;
    },
    //不通过理由
    refuseClick() {
      if (this.refuseText) {
        this.refuseId.loadNo = true;
        this.reviewAjax(this.refuseId, '3');
      } else {
        this.$message('请填写理由');
      }
    },
    //审核操作
    reviewAjax(item, status) {
      let query = {
        channelId: item.chId,
        status: status,
        reason: this.refuseText
      }
      channelStatus(query).then(res => {
        this.refuseFlg = false;
        item.loadOk = false;
        item.loadNo = false;
        if (res.code == 0) {
          this.getData();
        } else {
          this.$message(res.msg)
        }
      })
    },
    //账号停用
    dtChange(item) {
      channeluserStatus({
        id: item.chId,
        isOpen: item.chIsOpen
      }).then(res => {
        if (res.code == 0) {
          this.$message.success('操作成功');
        } else {
          this.$message(res.msg)
        }
        this.getData();
      })
    },
    //账号停用理由
    dtChangeClick() {
      this.dtLoad = true;
      if (this.dtText) {
        this.dtLoad = false;
        this.dtFlg = false;
      } else {
        this.$message('请填写理由');
      }
    },
    //切换审核
    activeClick() { },

    //序号
    tableIndex(index) {
      return (this.query.page - 1) * this.query.limit + index + 1
    },
    //选中
    handleSelectionChange() { },
    // 分页切换
    handlePageChange(val) {
      this.query.page = val;
      this.loading = true;
      this.getData();
    },
  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.el-select {
    width: 100%;
}
.tagChan {
    margin: 0 5px 5px 0;
}
</style>
